package com.string.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
//通知
@Component("jdbcAdvice")
@Aspect
public class JDBCAdvice {
    @Pointcut("execution(* com.string.service.*.get*(..))")
    public void pt(){}
@Before("pt()")
    public void beginTranslstion(){
        System.out.println("开启事务！");
    }
    @AfterReturning("pt()")
    public void commit(){
        System.out.println("提交事务！");
    }
    @AfterThrowing("pt()")
    public void rollback(){
        System.out.println("回滚事务！");
    }
    @After("pt()")
    public void finall(){
        System.out.println("释放事务！");
    }
    //环绕通知
    public Object around(ProceedingJoinPoint joinPoint){
        Object result=null;
        try{
            //开启事务
            beginTranslstion();
            //执行代理方法
            result=joinPoint.proceed(joinPoint.getArgs());
            //提交事务
            commit();
        } catch (Throwable e) {
            //回滚事务
            rollback();
            throw new RuntimeException(e);
        }finally {
            //释放资源
        }
        return result;
    }
}
